﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Microsoft.Win32;
using Maticsoft.Common;

namespace WEB.knowledge
{
    public partial class knowledgeRead : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string docName = "";
                if (Request.QueryString["docName"] != null)
                {
                    LinkButton1.Visible = true;
                    LinkButton1.Enabled = true;
                    docName = Request.QueryString["docName"];                    
                }
                else if (Request.QueryString["docName1"] != null)
                {
                    LinkButton2.Visible = true;
                    LinkButton2.Enabled = true;
                    docName = Request.QueryString["docName1"];
                }
                Session["fileName"] = docName;   //保存为全局变量
                string ex = System.IO.Path.GetExtension(docName);
                if (ex == ".html" || ex == ".txt")
                {
                    string docPath = docName.Substring(0, docName.LastIndexOf(@"\"));
                    string subfile = docPath.Substring(docPath.LastIndexOf(@"\") + 1);
                    using (System.IO.StreamReader sr = new StreamReader(docName, System.Text.Encoding.Default))
                    {
                        string word＿content = sr.ReadToEnd();
                        word＿content = word＿content.Replace("src=\"", "src=\"" + "../document/" + subfile + "/");//更新图片链接
                        word＿content = Server.HtmlDecode(word＿content);
                        //Response.Write(word＿content.ToString());
                        //Response.End();//停止输出
                        lblContent.Text = word＿content;
                    }
                }
                else
                {
                    //docName = docName.Replace(@"\", @"/");
                    Response.ClearContent();//清空缓冲区内容
                    Response.ClearHeaders();//清空缓冲区HTTP头
                    Response.ContentType = "application/PDF";//设置HTTP头为PDF文档其它类似
                    Response.WriteFile(docName);//写入客户端
                    //Response.Flush();//客户更新
                    Response.End();//写入关闭
                }
            }
        }

        protected void btnDown_Click(object sender, EventArgs e)
        {
            string fileName = Session["fileName"].ToString();
            if (System.IO.Path.GetExtension(fileName) == ".html")
            {
                fileName = fileName.Replace(".html", ".doc");
            }
            DownloadFile(fileName);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName = Session["fileName"].ToString();
                if (System.IO.Path.GetExtension(fileName) == ".html")
                {
                    string fileNameHtml = fileName;
                    fileName = fileName.Replace(".html", ".doc");
                    System.IO.FileInfo file = new System.IO.FileInfo(fileName); //原文件
                    System.IO.FileInfo file2 = new System.IO.FileInfo(fileNameHtml);
                    if (file.Exists) file.Delete();//删除doc
                    if (file2.Exists) file2.Delete();//删除HTML
                    string aimPath = fileName.Replace(".doc", ".files");
                    //删除文件夹
                    System.IO.Directory.Delete(aimPath, true);
                }
                else
                {
                    System.IO.FileInfo file = new System.IO.FileInfo(fileName); //原文件
                    if (file.Exists) file.Delete();
                }
                MessageBox.ShowAndRedirect(this, "删除成功!", "knowledgeView.aspx");
            }
            catch (Exception ex)
            {
                MessageBox.ShowAndRedirect(this, "删除不成功，请检查权限!", "knowledgeView.aspx");
            }
        
        }

        //// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filename">文件物理地址</param>
        protected void DownloadFile(string filename)
        {
            int intStart = filename.LastIndexOf(@"\")+1;
            string saveFileName = filename.Substring(intStart,filename.Length-intStart);

            System.IO.FileInfo fi=new System.IO.FileInfo(filename);
            string fileextname=fi.Extension;
            string DEFAULT_CONTENT_TYPE = "application/unknown";
            RegistryKey regkey,fileextkey;
            string filecontenttype;
            try 
            { 
                regkey=Registry.ClassesRoot; 
                fileextkey=regkey.OpenSubKey(fileextname); 
                filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString();
            }
            catch
            {
                filecontenttype=DEFAULT_CONTENT_TYPE;
            } 
            Response.Clear();
            Response.Charset = "gb2312";
            Response.Buffer= true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.Default;

            Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName); 
            Response.ContentType=filecontenttype;

            Response.WriteFile(filename); 
            Response.Flush();
            Response.Close();

            Response.End();
         }
    }
}
